package cn.iocoder.yudao.module.contract.service.company;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import cn.iocoder.yudao.module.contract.dal.dataobject.frameworkAgreement.FrameworkAgreementDO;
import cn.iocoder.yudao.module.contract.dal.mysql.frameworkAgreement.FrameworkAgreementMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.company.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.company.CompanyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.company.CompanyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 公司 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyDO> implements CompanyService {

    @Resource
    private CompanyMapper companyMapper;

    @Override
    public Long createCompany(CompanySaveReqVO createReqVO) {
        //校验唯一
        validateUnique(createReqVO.getId(),createReqVO.getCompanyCode());
        // 插入
        CompanyDO company = BeanUtils.toBean(createReqVO, CompanyDO.class);
        companyMapper.insert(company);
        // 返回
        return company.getId();
    }

    @Override
    public void updateCompany(CompanySaveReqVO updateReqVO) {
        // 校验存在
        validateCompanyExists(updateReqVO.getId());
        //校验唯一
        validateUnique(updateReqVO.getId(),updateReqVO.getCompanyCode());
        // 更新
        CompanyDO updateObj = BeanUtils.toBean(updateReqVO, CompanyDO.class);
        companyMapper.updateById(updateObj);
    }

    @Override
    public void deleteCompany(Long id) {
        // 校验存在
        validateCompanyExists(id);
        // 删除
        companyMapper.deleteById(id);
    }

    private void validateCompanyExists(Long id) {
        if (companyMapper.selectById(id) == null) {
            throw exception(COMPANY_NOT_EXISTS);
        }
    }
    @VisibleForTesting
    void validateUnique(Long id, String companyCode) {
        if (StrUtil.isBlank(companyCode)) {
            return;
        }
        CompanyDO data = companyMapper.selectOne(new LambdaQueryWrapper<CompanyDO>().eq(CompanyDO::getCompanyCode,companyCode));
        if (data == null) {
            return;
        }
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (id == null) {
            throw exception(COMPANY_EXISTS);
        }
        if (!data.getId().equals(id)) {
            throw exception(COMPANY_EXISTS);
        }
    }

    @Override
    public CompanyDO getCompany(Long id) {
        return companyMapper.selectById(id);
    }

    @Override
    public PageResult<CompanyDO> getCompanyPage(CompanyPageReqVO pageReqVO) {
        return companyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CompanyDO> getCompanyList(CompanyDO company) {
        return companyMapper.selectList( new LambdaQueryWrapperX<CompanyDO>()
                .likeIfPresent(CompanyDO::getCompanyCode, company.getCompanyCode())
                .likeIfPresent(CompanyDO::getCompanyName, company.getCompanyName())
                .orderByDesc(CompanyDO::getId));
    }

}